home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / TranslateQDShape.c < prev   
Encoding:
C/C++ Source or Header  |  1996-03-20  |  6.8 KB  |  259 lines  |  [TEXT/MPS ]

  1. /*
  2.     Sample code for parsing QuickDraw GX "QuickDraw PICT" shapes,
  3.     such as those which are rolled into print files when non-GX
  4.     applications print.
  5.  
  6.     8/30/94 - dmh - Universalized.
  7.     
  8.     Copyright © Apple Computer, 1994
  9. */
  10.  
  11. #include <Menus.h>
  12. #include <Memory.h>
  13. #include <String.h>
  14. #include <ToolUtils.h>
  15. #include <Resources.h>
  16. #include <Script.h>
  17. #include <Errors.h>
  18. #include <Font Routines.h>
  19. #include <Aliases.h>
  20. #include <QuickDraw.h>
  21. #include <Graphics Types.h>
  22. #include <Graphics Routines.h>
  23. #include <Graphics Errors.h>
  24. #include <Math Routines.h>
  25. #include <Graphics Toolbox.h>
  26. #include <GXExceptions.h>
  27.  
  28. // GrafPort wrapper for doing the translation so bottleneck can get to necessary information.
  29.  
  30. #define kQDBufferSize 1024
  31.  
  32. typedef struct {
  33.  
  34.     CGrafPort            thePort;                                // The graf port
  35.     short                    refnum;                                // file refnum.
  36.     unsigned char        buffer[kQDBufferSize];            // input buffer.
  37.     long                    bufferPosition;                    // buffer position.
  38.     long                    bufferSize;                            // Size of buffer.
  39.     OSErr                    status;                                // Place to stick error.
  40.  
  41. } TQDTranslationGrafPort;
  42.  
  43.  
  44. OSErr QGXStreamReadBuffer(TQDTranslationGrafPort *translationPort)
  45.     {
  46.         OSErr            status;
  47.         long            count;
  48.                     
  49.         count = kQDBufferSize;
  50.         
  51.         status = FSRead(translationPort->refnum, &count, &(translationPort->buffer[0]));
  52.         translationPort->bufferSize = count;
  53.         translationPort->bufferPosition = 0;
  54.         
  55. failed_read:
  56.         if (status == eofErr)            // end of file is okay.
  57.             status = noErr;
  58.             
  59.         ncheck(status);
  60.         return(status);
  61.         
  62.     }//QGXStreamReadBuffer
  63.  
  64.  
  65. /************
  66.     BottleNeck for spooling a PICT from disk
  67. **************/
  68. pascal void QGXGetPicProc(unsigned char *data, short count)
  69.     {
  70.         OSErr                            status = noErr;
  71.         long                            remaining;
  72.         long                            copy;
  73.         Ptr                            dataPtr;
  74.         TQDTranslationGrafPort    *translationPort;
  75.                 
  76.         GetPort(&(GrafPtr)translationPort);
  77.         
  78.         remaining = count;
  79.         dataPtr = data;
  80.         while (remaining > 0) {
  81.         
  82.             if ( (translationPort->bufferSize - translationPort->bufferPosition) >= remaining) {
  83.             
  84.                 /** There is enough in the buffer to finish **/
  85.             
  86.                 BlockMove(&(translationPort->buffer[translationPort->bufferPosition]), dataPtr, remaining);
  87.                 translationPort->bufferPosition += remaining;
  88.                 remaining = 0;
  89.             
  90.             } else {
  91.         
  92.                 /** Copy the remainder of the buffer, read a new buffer **/
  93.  
  94.                 copy = translationPort->bufferSize - translationPort->bufferPosition;            
  95.                 BlockMove(&(translationPort->buffer[translationPort->bufferPosition]), dataPtr, copy);
  96.                 remaining -= copy;
  97.                 dataPtr += copy;
  98.                 
  99.                 status = QGXStreamReadBuffer(translationPort);
  100.                 nrequire(status, failed_read);
  101.                             
  102.             }//end if
  103.                     
  104.         }//end while
  105.  
  106. failed_read:
  107.         
  108.         translationPort->status = status;
  109.         
  110.         /** Maybe this will make QuickDraw stop drawing the picture **/
  111.         
  112.         if (status != noErr)
  113.             memset(data, 0xFF, (long)count);
  114.         
  115.         
  116.     }//QGXGetPicProc
  117.  
  118.  
  119. /*****************************************************
  120.  
  121.     GXTranslateQuickDrawPict:
  122.     
  123.     Routine is called by a client to get individual
  124.     GX shapes from the QD pict.  The shapes are handed 
  125.     to the client using the callback and the refcon in 
  126.     the same manner as installing the translator on a
  127.     grafPort.
  128.     
  129.     This routine makes it easy for any client to deal
  130.     with QD shapes.
  131.     
  132.     source:        The source QD shape.
  133.     
  134. ******************************************************/
  135. OSErr QGXTranslateQuickDrawPict(gxShape source, gxShapeSpoolUPP userFunction, long refCon)
  136.     {
  137.         OSErr                            status, saveStatus;
  138.         GrafPtr                        curPort;
  139.         TQDTranslationGrafPort    *translationPort;
  140.         gxTag                            geometryTag;
  141.         gxQuickDrawPict            *shapeGeometry;
  142.         FSSpec                        fileSpec;
  143.         AliasHandle                    hAlias;
  144.         short                            fileCount = 1;
  145.         short                            refnum;
  146.         CQDProcs                        spoolProcs;
  147.         PicHandle                    pictToDraw;
  148.         Rect                            destRect;
  149.         gxRectangle                    theRectangle;
  150.         
  151.         require_action((QGXGetShapeType(source) == gxQuickDrawPictTag), failed_shapeType, status = -9999;);
  152.  
  153.         /** Make a quickdraw version of the rectangle for the destRect **/
  154.         
  155.         GXGetRectangle(source, &theRectangle);
  156.         destRect.top = theRectangle.top >> 16;
  157.         destRect.bottom  = theRectangle.bottom >> 16;
  158.         destRect.left = theRectangle.left >> 16;
  159.         destRect.right = theRectangle.right >> 16;
  160.         
  161.         /******* Get the QuickDraw pict information from the shape ********/        
  162.  
  163.         GXGetShapeTags(source, gxQuickDrawPictTag, 1, 1, &geometryTag);
  164.         GXLockTag(geometryTag);
  165.         shapeGeometry = GXGetTagStructure(geometryTag, nil);
  166.         
  167.         nrequire(status = GXGetGraphicsError(nil), failed_getAndLockTag);    
  168.  
  169.         /* Make an alias handle from the data in the tag */
  170.         
  171.         hAlias = (AliasHandle) TempNewHandle(shapeGeometry->alias.aliasRecordSize, &status);
  172.         nrequire(status, failed_newHandle);
  173.         
  174.         BlockMove((Ptr)&(shapeGeometry->alias.aliasRecord[0]), *hAlias, shapeGeometry->alias.aliasRecordSize);
  175.         
  176.         /* Get the file spec from the alias handle */
  177.         {
  178.         Boolean    needsUpdate = false;
  179.         status = MatchAlias(    nil, kARMSearch + kARMNoUI + kARMMountVol,
  180.                                     hAlias, &fileCount, &fileSpec, &needsUpdate, nil, nil);
  181.         }
  182.         nrequire(status, failed_Match);
  183.         
  184.         /* Now open the file */
  185.         
  186.         status = FSpOpenDF(&fileSpec, fsRdPerm, &refnum);
  187.         nrequire(status, failed_open);
  188.         
  189.         /* Set the file position */
  190.  
  191.         status = SetFPos(refnum, fsFromStart, shapeGeometry->alias.fileOffset);
  192.         nrequire(status, failed_setPos);
  193.         
  194.         /****** Set up a port for the translator with our buffer in it ******/
  195.  
  196.         translationPort = (TQDTranslationGrafPort *) NewPtrSys(sizeof(TQDTranslationGrafPort));
  197.         nrequire((status = MemError()), failed_NewPtr);
  198.         
  199.         translationPort->bufferPosition = 0;
  200.         translationPort->bufferSize = 0;
  201.         translationPort->refnum = refnum;
  202.         GetPort(&curPort);
  203.         OpenCPort((CGrafPtr)translationPort);
  204.         SetPort((GrafPtr)translationPort);
  205.         SetStdCProcs(&spoolProcs);
  206.         spoolProcs.getPicProc = NewQDGetPicProc(QGXGetPicProc);
  207.         translationPort->thePort.grafProcs = &spoolProcs;
  208.         
  209.         /* Draw the data through the translator */
  210.  
  211.         pictToDraw = (PicHandle) TempNewHandle(sizeof(Picture), &status);
  212.         nrequire(status, failed_newPicHandle);
  213.         (*pictToDraw)->picFrame = shapeGeometry->srcRect;
  214.     
  215.         GXInstallQDTranslator((GrafPtr)translationPort, shapeGeometry->options, 
  216.                                                                         &shapeGeometry->srcRect,
  217.                                                                         &destRect,
  218.                                                                         shapeGeometry->styleStretch,
  219.                                                                         userFunction, 
  220.                                                                         (void*)refCon);    
  221.         
  222.         DrawPicture(pictToDraw, &shapeGeometry->srcRect);
  223.     
  224.         (void)GXRemoveQDTranslator((GrafPtr)translationPort, nil);
  225.  
  226.         DisposeHandle((Handle)pictToDraw);
  227.         
  228. failed_newPicHandle:
  229.  
  230.         SetPort(curPort);
  231.  
  232.         CloseCPort((CGrafPtr)translationPort);
  233.         DisposePtr((Ptr)translationPort);
  234.  
  235. failed_NewPtr:
  236. failed_setPos:
  237.  
  238.         saveStatus = FSClose(refnum);
  239.         ncheck(saveStatus);
  240.         if (status == noErr)
  241.             status = saveStatus;
  242.  
  243. failed_open:
  244. failed_Match:
  245.                 
  246.         DisposeHandle((Handle)hAlias);
  247.         
  248. failed_newHandle:
  249.  
  250.         GXUnlockTag(geometryTag);
  251.         
  252. failed_getAndLockTag:
  253. failed_shapeType:
  254.  
  255.         return(status);
  256.     
  257.     }//QGXTranslateQuickDrawPict
  258.  
  259.